import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/utils/log_util.dart';
import 'package:jiffy/jiffy.dart';


/// 图标位置
enum SelectIconAlignment {
  left,
  right,
}

/// 选取时间段 返回开始时间和结束时间
/// [onConfirm] 开始时间 结束时间
/// [pattern] 时间格式 默认 yyyy-MM-dd
/// [startTime] 选择的开始时间
/// [endTime] 选择的结束时间
/// [iconCalendar] 选择的时间图标
/// [needSelectAll] 是否需要全选
class CustomSelectTimePeriod extends StatefulWidget {
  const CustomSelectTimePeriod(
      {required this.onConfirm,
      this.pattern = 'yyyy-MM-dd',
      this.startTime = '',
      this.endTime = '',
      this.iconCalendar = AppImages.icon_calendar,
        this.showBorder = false,
        this.textSize =  14,
        this.iconAlignment = SelectIconAlignment.left,
        this.needSelectAll = true,
      super.key, this.initTime});

  final Function(String startTime, String endTime) onConfirm;
  final String pattern;
  final String startTime;
  final String endTime;
  final String iconCalendar;
  final bool showBorder;
  final double textSize;
  final SelectIconAlignment iconAlignment;
  final bool needSelectAll;
  final DateTime? initTime;
  @override
  State<CustomSelectTimePeriod> createState() => _CustomSelectTimePeriodState();
}

class _CustomSelectTimePeriodState extends State<CustomSelectTimePeriod> {
  // 开始时间
  String _startDate = '开始时间';

  // 结束时间
  String _endDate = '结束时间';

  @override
  initState() {
    super.initState();
    if(widget.startTime.isNotEmpty){
      _startDate = widget.startTime;
    }
    if(widget.endTime.isNotEmpty){
      _endDate = widget.endTime;
    }
  }

  // 选择开始时间
  void _selectStartDate() {
    showDatePicker(
      context: context,
      initialDate: widget.startTime.isEmpty ? DateTime.now() : DateTime.parse(widget.startTime),
      firstDate: widget.initTime ?? DateTime(2010),
      lastDate: DateTime(2100),
    ).then((value) {
      if (value != null) {
        _startDate = Jiffy.parseFromDateTime(value).format(pattern: widget.pattern);
        if (_endDate != '结束时间') {
          widget.onConfirm(_startDate, _endDate);
          AppLogger.log('$_startDate----$_endDate');
        }else if(!widget.needSelectAll){
          widget.onConfirm(_startDate, '');
        }
        setState(() {});
      }
    });
  }

  // 选择结束时间
  void _selectEndDate() {
    showDatePicker(
      context: context,
      initialDate: widget.endTime.isEmpty ? DateTime.now() : DateTime.parse(widget.endTime),
      firstDate:widget.initTime ?? DateTime(2010),
      lastDate: DateTime(2100),
    ).then((value) {
      if (value != null) {
        _endDate = Jiffy.parseFromDateTime(value).format(pattern: widget.pattern);
        if (_startDate != '开始时间') {
          widget.onConfirm(_startDate, _endDate);
          AppLogger.log('$_startDate----$_endDate');
        }else if(!widget.needSelectAll){
          widget.onConfirm('', _endDate);
        }
        setState(() {});
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(left: 12.w, right: 12.w, top: 6.w, bottom: 6.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8.w),
        border: widget.showBorder ? Border.all(
          color: AppColors.font_color_gray.withOpacity(0.3),
          width: 1,
        ) : null,
      ),
      child: Row(
        children: [
          widget.iconAlignment == SelectIconAlignment.left ? SvgPicture.asset(widget.iconCalendar) :
          Container(),
          Expanded(
            child: Text(
              _startDate,
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: widget.textSize,
                fontWeight: FontWeight.w400,
                color: _startDate == '开始时间' ? const Color(0xffA0A7B5) : const Color(0xff666666),
              ),
            ).click(_selectStartDate),
          ),
          AppGaps.hGap5,
          Text(
            '至',
            style: TextStyle(
              fontSize: 14.sp,
              fontWeight: FontWeight.w400,
              color: AppColors.font_color_black,
            ),
          ),
          AppGaps.hGap5,
          Expanded(
            child: Text(
              _endDate,
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: widget.textSize,
                fontWeight: FontWeight.w400,
                color: _endDate == '结束时间' ? const Color(0xffA0A7B5) : const Color(0xff666666),
              ),
            ).click(_selectEndDate),
          ),
          widget.iconAlignment == SelectIconAlignment.right ? SvgPicture.asset(widget.iconCalendar) :
          Container(),
        ],
      ),
    );
  }
}
